function [] = guionoff(h_parent, on_off, varargin);

% GUIONOFF - enable/disable specified controls
%
%    GUIONOFF(H_OBJ, 'on', P1, V1, P2, V2, ...) enables objects
%    residing in the parent object H_OBJ.  The objects to be
%    enabled possess property values matching those specified by
%    the property name/value pairs Pi/Vi.  The property name Pi
%    must be a string and the property values Vi are cell arrays
%    containing the property value for each item to be checked.
%
%    GUIONOFF(H_OBJ, 'off', P1, V1, P2, V2, ...) disables the
%    specified objects.
%
%    NOTE:  the bitmap serving as the control label is either
%    grayed-out or restored for every UICONTROL object having
%    a label.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: August 31, 1999

% default button label colors (grayscale levels)

button_background = uint8(191);
disable_foreground = uint8(165);

% check input parameters

if (nargin < 4)
   msgbox('Insufficient number of arguments', 'GUIONOFF Error', 'warn');
   return
elseif (~ishandle(h_parent))
   msgbox('H_OBJ must be a valid object handle', 'GUIONOFF Error', 'warn');
   return
elseif (isempty(get(h_parent, 'Children')))
   msgbox('H_OBJ must have child objects', 'GUIONOFF Error', 'warn');
   return
elseif (~ischar(on_off))
   msgbox('ON/OFF flag must be a string', 'GUIONOFF Error', 'warn');
   return
elseif (isempty(find(strcmp({'on', 'off'}, on_off))))
   msgbox('ON/OFF flag must equal ''on'' or ''off''', 'GUIONOFF Error', 'warn');
   return
end

% find objects with specified property name/value pairs
% enable/disable objects that match property name/value pairs

h_obj = mfindobj(h_parent, varargin{:});
for i = 1 : length(h_obj)
   if (~strcmp(get(h_obj(i), 'Enable'), on_off))
      set(h_obj(i), 'Enable', on_off);
      if (istype(h_obj(i), 'uicontrol'))
         
         % restore old bitmap if enabling UICONTROL
         % check for valid RGB bitmap first
         
         if (strcmp(on_off, 'on'))
            bmap = get(h_obj(i), 'UserData');
            if (~isempty(bmap))
               if (length(size(bmap)) == 3)
                  if (size(bmap, 3) == 3)
                     set(h_obj(i), 'CData', bmap);
                     set(h_obj(i), 'UserData', []);
                  end
               end
            end
            
         % gray out bitmap label if disabling UICONTROL
         % check for valid RGB bitmap first
            
         elseif (strcmp(on_off, 'off'))
            bmap = get(h_obj(i), 'CData');
            if (~isempty(bmap))
               if (length(size(bmap)) == 3)
                  if (size(bmap, 3) == 3)
                     set(h_obj(i), 'UserData', bmap);
                     bmap(find(bmap ~= button_background)) = disable_foreground;
                     set(h_obj(i), 'CData', bmap);
                  end
               end
            end
         end
      end
   end
end
return
